<%@page import="entities.SiteConfig"%>
<%@page import="systems.ConfigSystem"%>
<%@page import="systems.AdsSystem"%>
<%@page import="entities.Category"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="entities.Author"%>
<%@page import="java.util.Map"%>
<%@page import="entities.Question"%>
<%@page import="java.util.List"%>
<%
SiteConfig siteConfig = ConfigSystem.getConfigs();
List<Question> questions = (List<Question>) request.getAttribute("questions");
Map<String, Author> authors = (Map<String, Author>) request.getAttribute("authors");
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
Author author = null;
Category category = null;

//Paginator
int limit = (Integer) request.getAttribute("limit");
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/default_lang/layouts/header.jsp" />
		<div class="mainContent clearafter">
			<div class="leftMainContent">
				<div class="adv top">
					<%=AdsSystem.getAds468x60() %>
				</div>
				
				<!-- TAB -->
				<ul id="tab" class="clearafter">
					<li class="active"><a href="/">Lastest Question</a></li>
					<li><a href="/recently">Recently Question</a></li>
				</ul>
				<!-- END TAB -->
				
				<!-- Content Tab -->
				<div class="content">
					<div class="content_in">
						<!-- question adv -->
						<div class="question_item clearafter">
							<span class="total_answers">
								<a href="javascript: return false">0</a> Answers
							</span>
							<div class="adv">
								<%=AdsSystem.getAds468x60() %>
							</div>
						</div>
						<!-- end question adv -->
						
						<% 
						for (int i = 0; i < questions.size(); i++) {
							if (i >= limit) break;
							if (!questions.get(i).getAuthor().isEmpty()) {
								author = authors.get(questions.get(i).getAuthor());
							} else {
								author = null;
							}
							
							if (!questions.get(i).getCategory().isEmpty()) {
								category = categories.get(questions.get(i).getCategory());
							} else {
								category = null;
							}
						%>
						
						<!-- question_item -->
						<div class="question_item clearafter">
							<span class="total_answers">
								<a href="/question/<%=questions.get(i).getId()%>"><%=questions.get(i).getAnswers().size() %></a> Answers
							</span>
							<h3>
								<a href="/question/<%=questions.get(i).getId()%>" title="<%=questions.get(i).getTitle()%>"><%=questions.get(i).getTitle() %></a>	
							</h3>
							<p>
								<% if (author != null) { %>
								<span class="user">
									Asked by: 
									<strong>
										<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author.getName() %></a>
									</strong>
								</span>
								<% } else if(!questions.get(i).getAuthorName().isEmpty()) { %>
									
								<% }%>
								<% if (category != null) { %>
								<span class="pcate">
									<a rel="tag" href="/category/<%=category.getAlias() %>-<%=category.getId()%>"><%=category.getName() %></a><br>
								</span>
								<% } %>
							</p>
							<% if (i < 2) { %>
							<div class="adv">
								<%=AdsSystem.getAds468x15() %>
							</div>
							<% } %>
						</div>
						<!-- end question_item -->
						<% } %>
						
						<!-- pagination -->
						<div class="pagination">
							<%
							List<Integer> range = paginator.getRangePage();
							%>
							<% if (prevPage) { %>
							<a href="<%=paginator.getPrev()%>">
								<strong>«</strong>
							</a>
							<% } %>
							
							<%
							if (nextPage || prevPage) {
								for (int i = 0; i < range.size(); i++) {
									if (range.get(i).intValue() == pages) {
							%>
								<strong class="on"><%=range.get(i).toString() %></strong>
								<%  } else if (nextPage || (!nextPage && range.get(i).intValue() <= pages)) { %>
								<a href="<%=paginator.getPage(range.get(i).intValue()) %>"><%=range.get(i).toString() %></a>
								<%  } %>
							<%  }
							} %>
							<% if (nextPage) { %>
							<a href="<%=paginator.getNext()%>">»</a>
							<% } %>
						</div>
						<!-- end pagination -->
					</div>
				</div>
				<!-- End Content tab -->
			</div>
			<jsp:include page="/templates/default_lang/elements/right.jsp" />
		</div>
<jsp:include page="/templates/default_lang/layouts/footer.jsp" />